package day10.gec.线程通信3;

class Shower5{
    int count = 1;

    public void show() throws InterruptedException {
        for (char i = 'a'; i <= 'z'; i++) {
            synchronized (Object.class) {
                while(count != 1) {
                    Object.class.wait();//等待 其实是将此线程进入暂停
                }

                Thread.sleep(10);
                System.out.println(i);

                count = 2;
                Object.class.notify();
            }

        }
    }

    public void show2() throws InterruptedException {
        for (int i = 1; i < 27; i++) {
            synchronized (Object.class) {
                while(count != 2) {
                    Object.class.wait();//等待 其实是将此线程进入暂停
                }

                Thread.sleep(10);
                System.out.println(i);

                count = 1;
                Object.class.notify();
            }

        }
    }
}

public class ShowerDemo3 {
    public static void main(String[] args) {
        final Shower5 s = new Shower5();
        new Thread() {

            @Override
            public void run() {
                try {
                    s.show();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }.start();

        new Thread() {

            @Override
            public void run() {
                try {
                    s.show2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }.start();
    }
}
